import { IMetaData, IMetaProperty } from '../metadata/type';
import { INIT_TYPE_STATUS, TYPE_STATUS } from './constant';

/**
 * 将元数据的schema转化成实例
 * @param schema IMataData
 * @returns
 */
export function transfmSchemaValue(schema: any) {
  const _schema: IMetaData = schema;

  let result: any;

  if (schema.type === TYPE_STATUS.OBJECT) {
    result = {};
    const _properties: IMetaProperty[] = _schema.properties;
    for (let i = 0; i < _properties.length; i++) {
      switch (_properties[i].type) {
        case TYPE_STATUS.OBJECT:
          result[_properties[i].id] = transfmSchemaValue(_properties[i]);
          break;
        case TYPE_STATUS.ARRAY:
          result[_properties[i].id] = [];
          if (_properties[i].items?.type !== TYPE_STATUS.REF) {
            result[_properties[i].id].push(transfmSchemaValue(_properties[i].items));
          }
          break;
        case TYPE_STATUS.ENUM:
          result[_properties[i].id] = [];
          result[_properties[i].id] = _properties[i].enum?.map((v) => {
            return v.value;
          });
          break;
        default:
          result[_properties[i].id] = getConstantValue(INIT_TYPE_STATUS, _properties[i].type);
      }
    }
  }

  if (schema.type === TYPE_STATUS.ARRAY) {
    result = [];
    const _items: Omit<IMetaProperty, 'id'> | undefined = _schema.items;
    if (_items?.type && _items.type !== TYPE_STATUS.REF && typeof schema.id === 'string') {
      result.push(transfmSchemaValue(schema));
    }
  }
  return result;
}

/**
 * 将扁平化的数据结构转化成树状对象
 * @param schema
 */
export function _processArrayToTree(schemas: IMetaData[]) {
  let parentNode = findParentNode(schemas);
  console.log('parentNode', parentNode);
  parentNode = replaceRefId(parentNode, schemas);
  console.log('parentNode=======', parentNode);
  return parentNode;
}

/**
 * 替换refId的schema
 * @param p  IMataProperty | Omit<IMataProperty, 'id'>
 * @param schemas
 * @returns
 */
export function replaceRefId(parentNode: IMetaData, schemas: IMetaData[]) {
  let props: any;

  if (parentNode.type === TYPE_STATUS.OBJECT) {
    props = parentNode.properties;
  }

  if (props) {
    props.forEach((p: IMetaProperty, idx: number) => {
      // 如果是ref类型则加上其对象
      if (p.type === TYPE_STATUS.REF && p.refId) {
        props[idx] = findRefSchema(p, schemas);
      }
      if (p.type === TYPE_STATUS.ARRAY && p.items?.type === TYPE_STATUS.REF && p.items.refId && parentNode.id !== p.items.refId) {
        props[idx].items = findRefSchema(p.items, schemas);
      }
    });
  }
  return parentNode;
}

export function findRefSchema(p: any, schemas: IMetaData[]) {
  const refSchema = schemas.filter((v) => v.id === p.refId);
  let _p: any = refSchema[0];
  _p = replaceRefId(_p, schemas);
  if (p.id) {
    _p.id = p.id;
  }
  return _p;
}

/**
 * 找到父节点
 * @param schemas
 * @returns
 */
export function findParentNode(schemas: IMetaData[]) {
  // 子节点
  const childNode: any[] = [];

  for (let i = 0; i < schemas.length; i++) {
    const _schema = schemas[i];
    const _properties: IMetaProperty[] = _schema.properties;

    _properties.forEach((v: IMetaProperty) => {
      if (v.type === TYPE_STATUS.REF && v.refId && _schema.id !== v.refId) {
        childNode.push(v.refId);
      }
      if (v.type === TYPE_STATUS.ARRAY && v.items?.type === TYPE_STATUS.REF && v.items.refId && _schema.id !== v.items.refId) {
        childNode.push(v.items.refId);
      }
    });
  }

  const parentNode = schemas.filter((v) => childNode.findIndex((it) => it === v.id) === -1);

  return parentNode[0]!;
}

/**
 * 调用时不用把参数补全; getValue(array, key) 一样可以调用
 * @param array 数组
 * @param key 指定的数值
 * @returns {string|string|string}
 */
export function getConstantValue(array: any[], key: any, strKey?: string, strValue?: string): string {
  let result = '';
  let _strKey = 'id';
  let _strValue = 'value';
  if (strKey) {
    _strKey = strKey;
  }
  if (strValue) {
    _strValue = strValue;
  }
  if (array) {
    for (const item of array) {
      if (key === item[_strKey]) {
        result = item[_strValue];
      }
    }
  }
  return result;
}
